package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.IndustryDAO;
import com.junzhiit.nengji.shop.model.Industry;
import com.junzhiit.nengji.shop.model.enums.TypeLevel;

@Repository
public class IndustryDAOImpl extends AbstractBaseDAO<Industry> implements IndustryDAO {

	@Override
	public List<Industry> findAll(String parentId) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select * from industry where is_delete = ?");
		params.setInt(IsDelete.NO.toIntValue());
		if (StringUtils.isNotBlank(parentId)) {
			sql.append(" and parent_id = ?");
			params.setString(parentId);
		}
		sql.append(" order by parent_id,sort desc");
		return super.queryForList(sql.toString(), Industry.class, params);
	}

	@Override
	public List<Industry> getIndustryFullName(String parentId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select id,name,full_name,parent_id from industry where is_delete =? ");
		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNoneBlank(parentId)){
			sql.append(" and parent_id =?");
			params.setString("root");
		}else{
			sql.append(" and parent_id !=?");
			params.setString("root");
		}
		sql.append(" order by full_name");
		return super.queryForList(sql.toString(), Industry.class,params);
	}

	@Override
	public int checkName(Industry industry) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select count(id) from industry where name = ? and is_delete = ?");
		params.setString(industry.getName());
		params.setInt(IsDelete.NO.toIntValue());
		if(StringUtils.isNotBlank(industry.getParentId())){
			sql.append(" and parent_id = ?");
			params.setString(industry.getParentId());
		}else{
			sql.append(" and level = ?");
			params.setInt(TypeLevel.ONE.toIntValue());
		}
		if(StringUtils.isNotBlank(industry.getId())){
			sql.append(" and id <> ?");
			params.setString(industry.getId());
		}
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public String getMaxCode(String parentId) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select max(code) from industry where 1 = 1");
		if(StringUtils.isNotBlank(parentId)){
			sql.append(" and parent_id = ?");
			params.setString(parentId);
		}
		return super.queryForString(sql.toString(), params);
	}

	@Override
	public int getMaxSort(String parentId) {
		StatementParameter params = new StatementParameter();
		StringBuilder sql = new StringBuilder();
		sql.append("select ifnull(max(sort),0)  from industry where 1 = 1");
		if(StringUtils.isNotBlank(parentId)){
			sql.append(" and parent_id = ?");
			params.setString(parentId);
		}
		return super.queryForInt(sql.toString(), params);
	}

	@Override
	public Industry getIndustryById(String id) {
		String sql = "select * from industry where id =? and is_delete =?";
		StatementParameter params = new StatementParameter();
		params.setString(id);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql,Industry.class,params);
	}

	@Override
	public List<Industry> getIndustryByParentId(String parentId) {
		String sql = "select * from industry where parent_id =? and is_delete =?";
		StatementParameter params = new StatementParameter();
		params.setString(parentId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql, Industry.class, params);
	}

}
